<?php
namespace intelligent\vcard\manage\controllers;

use Yii;
use intelligent\vcard\models\Vcard;
use services\Service;
use intelligent\vcard\services\VcardService;


/**
 * 名片相关
 */
class VcardController extends BaseController
{
    /**
     *  名片列表
     *
     * @return string
     */
    public function actionList()
    {
        $query = Vcard::find();
        return Service::toListPage($query);
    }

    /**
     * 添加名片
     *
     * @return string
     */
    public function actionAdd()
    {
        $reqeust = Yii::$app->getRequest();
        $params = $reqeust->post();
        if(empty($params)){
            return Service::fail('请求数据不能为空');
        }
        $userId = Yii::$app->getUser()->getId();
        $params['user_id'] = $userId;
        return VcardService::save($params);
    }

    /**
     * 修改名片
     *
     * @return string
     */
    public function actionUpdate($id)
    {
        $reqeust = Yii::$app->getRequest();
        $params = $reqeust->post();
        if(empty($params)){
            return Service::fail('请求数据不能为空');
        }
        $userId = Yii::$app->getUser()->getId();
        return VcardService::save($params, ['id'=>$id, 'user_id' => $userId]);
    }

    /**
     * 当前名片信息
     *
     * @return string
     */
    public function actionInfo()
    {

    }

    /**
     * 我的名片盒子
     *
     * @return string
     */
    public function actionMyCardBox()
    {
        $userId = Yii::$app->getUser()->getId();
        $query = Vcard::find();
        $query->where(['user_id' =>$userId]);
        return Service::toListPage($query);
    }
}
